<template>
  <div class="container">
    <el-dialog
      :title="params.id ? '修改目录' : '新增目录'"
      :visible.sync="showDialog"
      width="440px"
      @close="hClose"
    >
      <el-form ref="form" :model="params" label-width="80px" >
        <el-form-item
          label="所属学科"
          prop="subjectID"
          :rules="[
            { required: true, message: '请选择所属学科', trigger: 'blur' },
          ]"
        >
          <el-select
            v-model="params.subjectID"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="ele in options"
              :key="ele.value"
              :label="ele.label"
              :value="ele.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="目录名称"
          prop="directoryName"
          :rules="[
            { required: true, message: '请输入目录名称', trigger: 'blur' },
          ]"
        >
          <el-input
            v-model="params.directoryName"
            autocomplete="off"
            placeholder="请输入目录名称"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="hClose">取 消</el-button>
        <el-button type="primary" @click="hConfirm">确 认</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { simple } from '@/api/hmmm/subjects.js'
import { add, update } from '@/api/hmmm/directorys.js'
export default {
  data () {
    return {
      showDialog: false,
      options: [
        {
          value: '',
          label: ''
        }
      ],
      params: {
        subjectID: '',
        directoryName: ''
      }
    }
  },
  methods: {
    hClose () {
      this.showDialog = false
      this.params = {}
    },
    // 获取所属学科选项
    async getValue () {
      const { data } = await simple()
      // console.log(data)
      this.options = data
    },
    async hConfirm () {
      // 兜底
      await this.$refs.form.validate()
      this.params.id ? await update(this.params) : await add(this.params)
      this.$message.success(`${this.params.id ? '修改' : '新增'}成功`)
      this.showDialog = false
      // 调用父组件方法更新数组
      this.$emit('updateList')
    }
  },
  watch: {
    showDialog (val) {
      if (!val) {
        this.$refs.form.resetFields()
        this.params = {}
      } else {
        this.getValue()
      }
    }
  }
}
</script>

<style scoped lang='less'></style>
